package leetcode.binarysearch;
//35. 搜索插入位置
public class SearchInsert {
    public static void main(String[] args) {
        int[] data = {1,3,5,6};
        System.out.println(searchInsert(data, 4));
    }

    public static int searchInsert(int[] nums, int target) {
        int p = 0;
        int q = nums.length-1;
        int mid = 0;
        while (p<=q){
             mid = (p+q)/2;
            if (target<nums[mid]){
                q = mid-1;
            }else if (target>nums[mid]){
                p = mid+1;
            }else  {
                return mid;
            }
        }
        return p;
    }
}
